//
//  ASRPcmRecorder.h
//  testASR
//
//  Created by YangShuai on 16/9/22.
//  Copyright © 2016年 WanKang. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioQueue.h>
#import <AudioToolbox/AudioFile.h>
#import <AudioToolbox/AudioServices.h>
#import <AudioToolbox/AudioConverter.h>

#define NUM_BUFFERS 10
@class ASRPcmRecorder;
@protocol ASRPcmRecorderDelegate

/*
 * @回调音频数据，buffer表示音频数据，size表示音频的长度
 */
- (void) onReturnBuffer: (const void *)buffer bufferSize:(int)size;

/*
 * @音频的错误码
 */
- (void) onRecorderError:(ASRPcmRecorder*)recoder theError:(int) error;

/*
 * @回调音频能量 调用 getAudioPower 之后才有这个回调
 */
- (void) onRecorderPower:(float)power;

@end

@class ASRPcmRecorder;
typedef struct
{
    AudioFileID                 audioFile;
    AudioStreamBasicDescription dataFormat;
    AudioQueueRef               queue;
    AudioQueueLevelMeterState	*audioLevels;
    AudioQueueBufferRef         buffers[NUM_BUFFERS];
    UInt32                      bufferByteSize;
    SInt64                      currentPacket;
    BOOL                        recording;
    __unsafe_unretained ASRPcmRecorder             *recorder;
} RecordState;

@interface ASRPcmRecorder : NSObject
{
    Float64                     mSampleRate;		//采样率
    UInt32                      mBits;				//比特率
    UInt32                      mChannels;			//声道数
    
    Float32						_refreshHz;
    NSTimer						*_updateTimer;
    RecordState                 state;
    //id<ASRPcmRecorderDelegate> _delegate;
    
}

@property (nonatomic,assign) Boolean isTimerRuning;
@property (assign) id<ASRPcmRecorderDelegate> delegate;

/**
 * @fn      start
 * @brief   开始录音
 *
 * @return  开启录音成功返回YES，否则返回NO
 * @see
 */
- (BOOL) start;


/**
 * @fn      stop
 * @brief   停止录音
 *
 * @see
 */
- (void) stop;

/**
 * @fn      setSample
 * @brief   设置音频采样率
 *
 * @param   rate            -[in] 采样率，8k/16k
 * @see
 */
- (void) setSample:(int) rate;

- (void) getAudioPower;

@end

@interface ASRPcmRecorder(Extended)

- (void) setAudioFormat:(Float64)sampleRate theBitsPerChannel:(UInt32)bits theChannelsPerFrame:(UInt32)channels;

@end